/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.msk.wlhy.service;


import com.msk.common.utils.HtAssert;
import com.msk.sys.mapper.UserDao;
import com.msk.sys.service.CrudService;
import com.msk.wlhy.domain.HtSupplier;
import com.msk.wlhy.mapper.HtSupplierDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>[]</p>
 * <p>作者：shic</p>
 * <p>邮箱：shic@xiangyou.com</p>
 * <p>创建时间： 2019-03-29 </p>
 * <p>类说明：承运商管理Service </p>
 * <p>修改记录：</p>
 */
@Service
@Transactional(readOnly = true)
public class HtSupplierService extends CrudService<HtSupplierDao, HtSupplier> {

	@Autowired
	private UserDao userDao;

	public HtSupplier get(String id) {
		return super.get(id);
	}

	public List<HtSupplier> findList(HtSupplier htSupplier) {
		return super.findList(htSupplier);
	}

//	public Page<HtSupplier> findPage(Page<HtSupplier> page, HtSupplier htSupplier) {
//		return super.findPage(page, htSupplier);
//	}

	/**
	 * 获取无权限设置承运商列表数据
	 * @param htSupplier
	 * @return
	 */
//	public Page<HtSupplier> findListPage(Page<HtSupplier> page, HtSupplier htSupplier) {
//		return super.findPage(page, htSupplier);
//	}

	@Transactional(readOnly = false)
	public void save(HtSupplier htSupplier) {
		super.save(htSupplier);
	}

	@Transactional(readOnly = false)
	public void delete(HtSupplier htSupplier) {
		super.delete(htSupplier);
	}

	@Transactional(readOnly = false)
	public int deleteIds(String[] ids) {
		int result = 0;
		for (String id : ids) {
			super.delete(new HtSupplier(id));
			result ++;
		}
		return result;
	}

	@Transactional(readOnly = false)
	public void updateStatus(HtSupplier htSupplier){
		dao.updateStatus(htSupplier);
	}

	public long selectMaxSupCode(){
		return dao.selectMaxSupCode();
	}

	public HtSupplier getHtSupplierByUserId(String userid) {
		return dao.getSupplierByUserId(userid);
	}

	public int uniqSocialCode(@Param("unifiedSocialCreditCode") String unifiedSocialCreditCode, @Param("id") String id){
		return dao.uniqSocialCode(unifiedSocialCreditCode, id);
	}

	public int nameCheck(@Param("name") String name, @Param("id") String id){
		return dao.nameCheck(name, id);
	}

	/**
	 * 承运商 审核通过后，将企业名称与个人信息的名称进行同步
	 */
	@Transactional(readOnly = false)
	public void updateStatusNew(HtSupplier htSupplier) {
		dao.updateStatus(htSupplier);
		userDao.updateNameByUserId(htSupplier.getUserId(),htSupplier.getName());
	}

	public List<HtSupplier> getPassHtSuppliers(Map<String, Object> param){
		return dao.getPassHtSuppliers(param);
	}

	/**
	 * 根据用户名获取承运商
	 * @param loginName
	 * @return
	 */
	public HtSupplier getHtSupplierByLoginName(String loginName) {
		return dao.getHtSupplierByLoginName(loginName);
	}

	/**
	 * 更新用户的openId值
	 * @param
	 * @return List<M2mLoginUserDTO>
	 * @throws
	 */
	@Transactional(readOnly = false)
	public void updateM2mReguserOpenId(HtSupplier htSupplier) throws Exception {
		String openid = htSupplier.getOpenId();
		// 根据用户ID查询出该注册用户信息
		// 将open设置为空去查询数据
		htSupplier.setOpenId(null);
		List<HtSupplier> htSupplierList = findList(htSupplier);
		HtAssert.notEmpty(htSupplierList, "您的帐号登录发生异常，请联系系统管理员");
		HtAssert.outOfSize(htSupplierList, 1, "您的帐号登录发生异常，请联系系统管理员");
		HtSupplier htSupplierUpdate = htSupplierList.get(0);
		htSupplierUpdate.setOpenId(openid);
		super.save(htSupplierUpdate);
	}

	/**
	 * 解除绑定的微信号
	 * @param
	 * @param
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = false)
	public void removeOpenId(HtSupplier htSupplier) throws Exception {
		List<HtSupplier> list = findList(htSupplier);
		if (!CollectionUtils.isEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				HtSupplier htSupplierTemp = list.get(i);
				String[] openids = htSupplierTemp.getOpenId().split(",");
				List<String> openids1 = new ArrayList<String>();
				for (int j = 0; j < openids.length; j++) {
					String openid  =  openids[j];
					if(!openid.equals(htSupplier.getOpenId())){
						openids1.add(openid);
					}
				}

				StringBuffer sb = new StringBuffer();

				for (int j = 0; j < openids1.size(); j++) {
					if(j < openids1.size() - 1){
						sb.append(openids1.get(j) +",");
					}else{
						sb.append(openids1.get(j));
					}
				}
				htSupplierTemp.setOpenId(sb.toString());
				super.save(htSupplierTemp);
			}
		}
	}

	public List<Map<String , String>> getAllHtSuppliersJson(Map<String, Object> param){
		return dao.getAllHtSuppliersJson(param);
	}
}